{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import os\n",
    "\n",
    "# 读写文件路径定义\n",
    "file_path_read = os.path.abspath(os.path.join(os.path.dirname(__file__), './res/files/prog/student.csv'))\n",
    "file_path_write = os.path.abspath(os.path.join(os.path.dirname(__file__), './res/files/prog/write.csv'))\n",
    "\n",
    "index = -1\n",
    "lst_read = list()\n",
    "with open(file_path_write, 'w', newline='', encoding='UTF-8') as f_write:\n",
    "    with open(file_path_read, 'r', encoding='UTF-8') as f_read:\n",
    "        writer = csv.writer(f_write)\n",
    "        file = csv.reader(f_read)\n",
    "        for i in file:\n",
    "            lst_read.append(i)\n",
    "        sum_math = 0\n",
    "        for i in lst_read:\n",
    "            index += 1\n",
    "            if index != 0:\n",
    "                sum_math += int(i[1])\n",
    "        print(sum_math)\n",
    "\n",
    "        # index += 1\n",
    "        # if index != 0:\n",
    "        #     lst_temp = [i[0], int(i[1]) + int(i[2]) + int(i[3])]\n",
    "        #     writer.writerow(lst_temp)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 12.28\n",
    "- 计算`9+98+987+...+987654321`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 解答思路:\n",
    "  - 观察这个式子不难发现该式中的相邻项之间的联系,就是[1,9]倒序每次取一个加在最低位构成一项\n",
    "  - 因此可以按照上面这个思路构造每一项,将[1,9]倒序每次取一个数放在最低位构成一项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "累加前sum: 0 , 累加值:9\n",
      "累加后sum: 9\n",
      "累加前sum: 9 , 累加值:98\n",
      "累加后sum: 107\n",
      "累加前sum: 107 , 累加值:987\n",
      "累加后sum: 1094\n",
      "累加前sum: 1094 , 累加值:9876\n",
      "累加后sum: 10970\n",
      "累加前sum: 10970 , 累加值:98765\n",
      "累加后sum: 109735\n",
      "累加前sum: 109735 , 累加值:987654\n",
      "累加后sum: 1097389\n",
      "累加前sum: 1097389 , 累加值:9876543\n",
      "累加后sum: 10973932\n",
      "累加前sum: 10973932 , 累加值:98765432\n",
      "累加后sum: 109739364\n",
      "累加前sum: 109739364 , 累加值:987654321\n",
      "累加后sum: 1097393685\n"
     ]
    }
   ],
   "source": [
    "sum=0        # 累加器\n",
    "num_temp=0   # 当前值\n",
    "for i in range(9,0,-1):\n",
    "    print(\"累加前sum: {0} , 累加值:{1}\".format(sum,num_temp*10+i))\n",
    "    num_temp = num_temp*10+i\n",
    "    sum +=  num_temp\n",
    "    print(\"累加后sum:\",sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- `range(9,0,-1)`指的是从9到1,每次-1,也即是[1,9]的倒序\n",
    "  - 注意0是取不到的;相应的`range(0,9)`表示的范围是[0,9),9是取不到的\n",
    "    - `range`第三个参数默认是+1\n",
    "- `format`用于匹配字符串中的待指明数据{0}{1}...\n",
    "  - 这里写输出语句主要是为了直观地显示计算过程,只要结果的话把`print`语句去掉即可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该式值为:1097393685\n"
     ]
    }
   ],
   "source": [
    "sum=0        # 累加器\n",
    "num_temp=0   # 当前值\n",
    "for i in range(9,0,-1):\n",
    "    num_temp = num_temp*10+i\n",
    "    sum +=  num_temp\n",
    "print(\"该式值为:{0}\".format(sum))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc-autonumbering": false
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
